package com.ssy.lingxi.member.merchant.serviceimpl.feign;

import com.ssy.lingxi.common.constant.basic.EnableDisableStatus;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.model.vo.inner.request.MemberOrderCommentVO;
import com.ssy.lingxi.member.merchant.entity.MemberOrderCommentDO;
import com.ssy.lingxi.member.merchant.entity.MemberOrderProductCommentDO;
import com.ssy.lingxi.member.merchant.repository.MemberOrderCommentRepository;
import com.ssy.lingxi.member.merchant.repository.MemberOrderProductCommentRepository;
import com.ssy.lingxi.member.merchant.service.feign.IMemberOrderCommentFeignService;
import com.ssy.lingxi.member.merchant.service.feign.IMessageFeignService;
import com.ssy.lingxi.member.merchant.service.feign.IReportFeignService;
import com.ssy.lingxi.member.merchant.service.web.IMemberCommentScheduleService;
import com.ssy.lingxi.message.api.constant.MessageTemplateCode;
import com.ssy.lingxi.report.api.enums.OperateDataSourceEnum;
import com.ssy.lingxi.report.api.enums.PurchaseOrderOperateTypeEnum;
import com.ssy.lingxi.report.api.enums.SaleOrderOperateTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员订单评价feign服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/8/3
 */
@Service
public class MemberOrderCommentFeignServiceImpl implements IMemberOrderCommentFeignService {

    private static final Logger log = LoggerFactory.getLogger(ScheduleFeignServiceImpl.class);

    @Resource
    private MemberOrderCommentRepository memberOrderCommentRepository;

    @Resource
    private MemberOrderProductCommentRepository memberOrderProductCommentRepository;

    @Resource
    private IMemberCommentScheduleService memberCommentScheduleService;

    @Resource
    private IReportFeignService reportFeignService;

    @Resource
    private IMessageFeignService messageFeignService;

    /**
     * 保存订单数据
     * @param memberOrderCommentVO 接口参数
     * @return 返回结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> saveMemberOrderComment(MemberOrderCommentVO memberOrderCommentVO) {
        // 订单
        MemberOrderCommentDO memberOrderCommentDO = new MemberOrderCommentDO();
        memberOrderCommentDO.setId(memberOrderCommentVO.getId());
        memberOrderCommentDO.setCreateTime(memberOrderCommentVO.getCreateTime());
        memberOrderCommentDO.setBuyerMemberId(memberOrderCommentVO.getBuyerMemberId());
        memberOrderCommentDO.setBuyerRoleId(memberOrderCommentVO.getBuyerRoleId());
        memberOrderCommentDO.setBuyerMemberName(memberOrderCommentVO.getBuyerMemberName());
        memberOrderCommentDO.setVendorMemberId(memberOrderCommentVO.getVendorMemberId());
        memberOrderCommentDO.setVendorRoleId(memberOrderCommentVO.getVendorRoleId());
        memberOrderCommentDO.setVendorMemberName(memberOrderCommentVO.getVendorMemberName());
        memberOrderCommentDO.setOrderNo(memberOrderCommentVO.getOrderNo());
        memberOrderCommentDO.setShopId(memberOrderCommentVO.getShopId());
        memberOrderCommentDO.setShopType(memberOrderCommentVO.getShopType());
        memberOrderCommentDO.setShopEnvironment(memberOrderCommentVO.getShopEnvironment());
        memberOrderCommentDO.setShopName(memberOrderCommentVO.getShopName());
        memberOrderCommentDO.setOrderMode(memberOrderCommentVO.getOrderMode());
        memberOrderCommentDO.setOrderType(memberOrderCommentVO.getOrderType());
        memberOrderCommentDO.setOrderKind(memberOrderCommentVO.getOrderKind());
        memberOrderCommentDO.setPayType(memberOrderCommentVO.getPayType());
        memberOrderCommentDO.setPayChannel(memberOrderCommentVO.getPayChannel());
        memberOrderCommentDO.setQuoteNo(memberOrderCommentVO.getQuoteNo());
        memberOrderCommentDO.setDigest(memberOrderCommentVO.getDigest());
        memberOrderCommentDO.setProductAmount(memberOrderCommentVO.getProductAmount());
        memberOrderCommentDO.setFreight(memberOrderCommentVO.getFreight());
        memberOrderCommentDO.setPromotionAmount(memberOrderCommentVO.getPromotionAmount());
        memberOrderCommentDO.setCouponAmount(memberOrderCommentVO.getCouponAmount());
        memberOrderCommentDO.setTotalAmount(memberOrderCommentVO.getTotalAmount());
        memberOrderCommentDO.setBuyerInnerStatus(memberOrderCommentVO.getBuyerInnerStatus());
        memberOrderCommentDO.setVendorInnerStatus(memberOrderCommentVO.getVendorInnerStatus());
        memberOrderCommentDO.setOuterStatus(memberOrderCommentVO.getOuterStatus());
        memberOrderCommentDO.setBuyerCompleteCommentStatus(EnableDisableStatus.DISABLE.getCode());
        memberOrderCommentDO.setVendorCompleteCommentStatus(EnableDisableStatus.DISABLE.getCode());
        memberOrderCommentDO.setCompleteTime(LocalDateTime.now());
        memberOrderCommentRepository.saveAndFlush(memberOrderCommentDO);

        // 订单商品
        List<MemberOrderProductCommentDO> memberOrderProductCommentDOList = memberOrderCommentVO.getProducts().stream().map(product -> {
            MemberOrderProductCommentDO memberOrderProductCommentDO = new MemberOrderProductCommentDO();
            memberOrderProductCommentDO.setId(product.getId());
            memberOrderProductCommentDO.setOrder(memberOrderCommentDO);
            memberOrderProductCommentDO.setProductId(product.getProductId());
            memberOrderProductCommentDO.setSkuId(product.getSkuId());
            memberOrderProductCommentDO.setProductNo(product.getProductNo());
            memberOrderProductCommentDO.setName(product.getName());
            memberOrderProductCommentDO.setCategory(product.getCategory());
            memberOrderProductCommentDO.setBrand(product.getBrand());
            memberOrderProductCommentDO.setSpec(product.getSpec());
            memberOrderProductCommentDO.setUnit(product.getUnit());
            memberOrderProductCommentDO.setLogo(product.getLogo());
            memberOrderProductCommentDO.setPrice(product.getPrice());
            memberOrderProductCommentDO.setQuantity(product.getQuantity());
            memberOrderProductCommentDO.setAmount(product.getAmount());
            memberOrderProductCommentDO.setDeliverType(product.getDeliverType());
            memberOrderProductCommentDO.setAddress(product.getAddress());
            memberOrderProductCommentDO.setReceiver(product.getReceiver());
            memberOrderProductCommentDO.setPhone(product.getPhone());
            memberOrderProductCommentDO.setBuyerCommentStatus(EnableDisableStatus.DISABLE.getCode());
            memberOrderProductCommentDO.setVendorCommentStatus(EnableDisableStatus.DISABLE.getCode());
            return memberOrderProductCommentDO;
        }).collect(Collectors.toList());
        memberOrderProductCommentRepository.saveAll(memberOrderProductCommentDOList);

        // 异步创建定时评价任务
        memberCommentScheduleService.asyncOrderAutoComment(memberOrderCommentVO);

        // 异步采购首页统计
        reportFeignService.executeIncreaseReport(OperateDataSourceEnum.TRADE_PURCHASE_ORDER.getCode(), PurchaseOrderOperateTypeEnum.TO_BE_EVALUATE.getCode(),
                memberOrderCommentDO.getBuyerMemberId(), memberOrderCommentDO.getBuyerRoleId());

        // 异步采购消息
        messageFeignService.sendSystemMessage(memberOrderCommentDO.getBuyerMemberId(), memberOrderCommentDO.getBuyerRoleId(),
                MessageTemplateCode.procurement_order_comment, Arrays.asList(memberOrderCommentDO.getOrderNo(), memberOrderCommentDO.getDigest()));

        // 异步销售首页统计
        reportFeignService.executeIncreaseReport(OperateDataSourceEnum.TRADE_SALE_ORDER.getCode(), SaleOrderOperateTypeEnum.TO_BE_EVALUATE.getCode(),
                memberOrderCommentDO.getVendorMemberId(), memberOrderCommentDO.getVendorRoleId());

        // 异步供应消息
        messageFeignService.sendSystemMessage(memberOrderCommentDO.getVendorMemberId(), memberOrderCommentDO.getVendorRoleId(),
                MessageTemplateCode.sell_order_comment, Arrays.asList(memberOrderCommentDO.getOrderNo(), memberOrderCommentDO.getDigest()));


        return Wrapper.success();
    }
}
